package com.machine.service.user.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.machine.bean.Componentpart;
import com.machine.bean.Model;
import com.machine.bean.PageBean;
import com.machine.dao.user.ComponentDao;
import com.machine.service.user.ComponentServiceDao;

@Component(value = "componentService")
public class ComponentServiceDaoImpl implements ComponentServiceDao {

	private ComponentDao componentDao;

	public ComponentDao getComponentDao() {
		return componentDao;
	}

	@Resource
	public void setComponentDao(ComponentDao componentDao) {
		this.componentDao = componentDao;
	}

	@Override
	public Integer add(com.machine.bean.Componentpart component) {
		return componentDao.add(component);

	}

	@Override
	public int delete(com.machine.bean.Componentpart component) {
		return componentDao.delete(component);
	}

	@Override
	public List<com.machine.bean.Componentpart> find(String name,int offset, int length) {
		return componentDao.find(name, offset, length);
	}

	@Override
	public int update(com.machine.bean.Componentpart component) {
		return componentDao.update(component);
	}

	@Override
	public List<Componentpart> findAll() {
		return componentDao.findAll();
	}

	@Override
	public int deleteByIdList(List<Integer> idList) {

		return componentDao.deleteByIdList(idList);
	}

	@Override
	public Componentpart findById(Integer id) {
		return componentDao.findById(id);
	}

	@Override
	public PageBean queryForPage(int pageSize, int page,String username) {
		final String hql = "from Componentpart where providername='" + username+ "'"; // 查询语句
		int allRow = componentDao.getAllRowCount(hql); // 总记录数
		
		int totalPage = PageBean.countTotalPage(pageSize, allRow); // 总页数
		final int offset = PageBean.countOffset(pageSize, page); // 当前页开始记录
		final int length = pageSize; // 每页记录数
		final int currentPage = PageBean.countCurrentPage(page);
		List<Componentpart> list = componentDao.queryForPage(hql, offset,length); // "一页"的记录
		// 把分页信息保存到Bean中
		PageBean pageBean = new PageBean();
		pageBean.setPageSize(pageSize);
		pageBean.setCurrentPage(currentPage);
		pageBean.setAllRow(allRow);
		pageBean.setTotalPage(totalPage);
		pageBean.setList(list);
		pageBean.init();
		System.out.println("allrow : " + allRow);
		System.out.println("totalPage : " + totalPage);
		System.out.println("offset : " + offset);
		System.out.println("currentPage : " + currentPage);
		return pageBean;
	}

	@Override
	public PageBean querySearchForPage(String name,int pageSize, int page) {
		System.out.println("name : " + name);
		String hql = "from Componentpart componentpart where componentpart.name like '%"
			+ name + "%' order by componentpart.price";
	   // String hql = "from Componentpart where name like '" + name + "' "; // 查询语句
	    System.out.println("hql : " + hql);
		int allRow = componentDao.getAllRowCount(hql); // 总记录数
		int totalPage = PageBean.countTotalPage(pageSize, allRow); // 总页数
		int offset = PageBean.countOffset(pageSize, page); // 当前页开始记录
		int length = pageSize; // 每页记录数
		int currentPage = PageBean.countCurrentPage(page);
		List<Componentpart> list = componentDao.queryForPage(hql, offset,length); // "一页"的记录
		// 把分页信息保存到Bean中
		PageBean pageBean = new PageBean();
		pageBean.setPageSize(pageSize);
		pageBean.setCurrentPage(currentPage);
		pageBean.setAllRow(allRow);
		pageBean.setTotalPage(totalPage);
		pageBean.setList(list);
		pageBean.init();
		System.out.println("allrow : " + allRow);
		System.out.println("totalPage : " + totalPage);
		System.out.println("offset : " + offset);
		System.out.println("currentPage : " + currentPage);
		return pageBean;
	}

	@Override
	public PageBean querySearchForPage(String name, int pageSize,
			int page, String tag, String orderKey, String orderValue) {
		
		String hql="";
		
		if(orderKey != null&&!orderKey.equals("first")&&!tag.equals("")){
		
			if(orderKey.equals("price")){
				if(tag.equals("")){
					 if(orderValue.equals("0")){
						 hql = "from Componentpart componentpart where componentpart.name like '%"
									+ name + "%' order by componentpart.price asc";
					 }else{
						 hql = "from Componentpart componentpart where componentpart.name like '%"
									+ name + "%' order by componentpart.price desc";
					 }
				}else{
					if(orderValue.equals("0")){
						hql = "from Componentpart componentpart where componentpart.name like '%"
								+ name + "%' and componentpart.componentnumber-"+tag+">0 and componentpart.componentnumber-"+tag+"<100 order by componentpart.price asc";
					}else{
						hql = "from Componentpart componentpart where componentpart.name like '%"
								+ name + "%' and componentpart.componentnumber-"+tag+">0 and componentpart.componentnumber-"+tag+"<100 order by componentpart.price desc";
					}
				}

				
			}
			if(orderKey.equals("sell")){
				
				if(tag.equals("")){
					 if(orderValue.equals("0")){
						 hql = "from Componentpart componentpart where componentpart.name like '%"
									+ name + "%' order by componentpart.sellnum asc";
					 }else{
						 hql = "from Componentpart componentpart where componentpart.name like '%"
									+ name + "%' order by componentpart.sellnum desc";
					 }
				}else{
					if(orderValue.equals("0")){
						hql = "from Componentpart componentpart where componentpart.name like '%"
								+ name + "%' and componentpart.componentnumber-"+tag+">0 and componentpart.componentnumber-"+tag+"<100 order by componentpart.sellnum asc";
					}else{
						hql = "from Componentpart componentpart where componentpart.name like '%"
								+ name + "%' and componentpart.componentnumber-"+tag+">0 and componentpart.componentnumber-"+tag+"<100 order by componentpart.sellnum desc";
					}
				}
				

				
			}
			if(orderKey.equals("stockamount")){
				
				if(tag.equals("")){
					 if(orderValue.equals("0")){
						 hql = "from Componentpart componentpart where componentpart.name like '%"
									+ name + "%' order by componentpart.stockamount asc";
					 }else{
						 hql = "from Componentpart componentpart where componentpart.name like '%"
									+ name + "%' order by componentpart.stockamount desc";
					 }
				}else{
					if(orderValue.equals("0")){
						hql = "from Componentpart componentpart where componentpart.name like '%"
								+ name + "%' and componentpart.componentnumber-"+tag+">0 and componentpart.componentnumber-"+tag+"<100 order by componentpart.stockamount asc";
					}else{
						hql = "from Componentpart componentpart where componentpart.name like '%"
								+ name + "%' and componentpart.componentnumber-"+tag+">0 and componentpart.componentnumber-"+tag+"<100 order by componentpart.stockamount desc";
					}
				}
				
			}
			if(orderKey.equals("time")){
				if(tag.equals("")){
					 if(orderValue.equals("0")){
						 hql = "from Componentpart componentpart where componentpart.name like '%"
									+ name + "%' order by componentpart.outtime asc";
					 }else{
						 hql = "from Componentpart componentpart where componentpart.name like '%"
									+ name + "%' order by componentpart.outtime desc";
					 }
				}else{
					if(orderValue.equals("0")){
						hql = "from Componentpart componentpart where componentpart.name like '%"
								+ name + "%' and componentpart.componentnumber-"+tag+">0 and componentpart.componentnumber-"+tag+"<100 order by componentpart.outtime asc";
					}else{
						hql = "from Componentpart componentpart where componentpart.name like '%"
								+ name + "%' and componentpart.componentnumber-"+tag+">0 and componentpart.componentnumber-"+tag+"<100 order by componentpart.outtime desc";
					}
				}
			}
			
			if(orderKey.equals("type")&&!tag.equals("")){
				
				int tagNum = Integer.valueOf(tag);
				if(tagNum%100==0){
					hql = "from Componentpart componentpart where componentpart.name like '%"
							+ name + "%' and componentpart.componentnumber-"+tag+">0 and componentpart.componentnumber-"+tag+"<100";
					
				}else{
					 hql = "from Componentpart componentpart where componentpart.name like '%"
							+ name + "%' and componentpart.componentnumber-"+tag+"=0";
				}
			}
			
		}else{
			System.out.println("name : " + name);
		    hql = "from Componentpart componentpart where componentpart.name like '%"
				+ name + "%' order by componentpart.price";
		}
		
	   // String hql = "from Componentpart where name like '" + name + "' "; // 查询语句
	//    System.out.println("hql : " + hql);
		int allRow = componentDao.getAllRowCount(hql); // 总记录数
		int totalPage = PageBean.countTotalPage(pageSize, allRow); // 总页数
		int offset = PageBean.countOffset(pageSize, page); // 当前页开始记录
		int length = pageSize; // 每页记录数
		int currentPage = PageBean.countCurrentPage(page);
		List<Componentpart> list = componentDao.queryForPage(hql, offset,length); // "一页"的记录
		// 把分页信息保存到Bean中
		PageBean pageBean = new PageBean();
		pageBean.setPageSize(pageSize);
		pageBean.setCurrentPage(currentPage);
		pageBean.setAllRow(allRow);
		pageBean.setTotalPage(totalPage);
		pageBean.setList(list);
		pageBean.init();
		
		return pageBean;
	}

	@Override
	public int uploadModel(Model model) {
		return componentDao.uploadModel(model);
	}

	@Override
	public List<Model> findModel(String name, int offset, int length) {
		return componentDao.findModel(name, offset, length);
	}

	@Override
	public int addModel(int id, String path) {
		return componentDao.addModel(id, path);
	}

}
